<template>
  <div class="my-body">
    <el-row>
      <el-col :span="24">
        <div class="my-table-search">
          <el-form ref="form" :model="form">
            <el-row :gutter="10">
              <el-col :span="3">
                <el-input v-model="input" placeholder="请输入内容" />
              </el-col>

              <el-col :span="3">
                <el-button type="primary" @click="caidanlist" plain>查询</el-button>

                
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="my-table-tools">
          <div class="my-table-tools-left">
            <el-button type="primary" size="small" icon="el-icon-plus" plain @click="handleAddRole">新增</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" plain>删除</el-button>
          </div>
          <div class="my-table-tools-right">
            <el-button icon="el-icon-refresh" circle plain @click="refresh" />
          </div>
        </div>
      </el-col>
      <el-col :span="24">
        <div class="my-table-body">
          <el-table :data="roles" border style="width: 100%"  height="700" max-height="700"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}">
            <el-table-column prop="id" label="ID" width="180"></el-table-column>
            <el-table-column
              prop="name"
              label="部门名称"
            
            ></el-table-column>
            <el-table-column
              prop="department_code"
              label="部门编号"
            
            ></el-table-column>
            <!-- <el-table-column
          prop="name"
          label="创建时间"
          width="180"
        ></el-table-column> -->
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="handleEdit(scope.row)"
                  
                >
                  <i class="el-icon-edit"></i>编辑</el-button
                >

                <el-button
                  size="mini"
                  type="text"
                  @click="handleDelete(scope.row)"
                  class="my-btn-text-color-2"
                  ><i class="el-icon-delete"></i>删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="my-table-pagination">
          <el-pagination
            background
            :current-page="currentPage4"
            :page-sizes="[100, 200, 300, 400]"
            :page-size="100"
            layout="->, total, sizes, prev, pager, next, jumper"
            :total="400"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-col>
    </el-row>
    <el-dialog title="添加部门" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="newRole" label-width="80px">
        <el-form-item label="部门名称">
          <el-input v-model="newRole.name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiaotj">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑添加部门" :visible.sync="dialogVisiblebj" width="30%">
      <el-form ref="form" :model="newRole" label-width="80px">
        <el-form-item label="部门名称">
          <el-input v-model="newRoles.name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiaobj">取 消</el-button>
        <el-button type="primary" @click="addRolebj">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
     
    <script>
import { getData, postData } from "@/utils/request.js";
export default {
  data() {
    return {
      roles: [],
      newRole: { name: "" },
      newRoles: { name: "", id: "" },
      dialogVisible: false,
      dialogVisiblefenpei: false,
      dialogVisiblebj: false,
      data: [
        {
          id: 1,
          label: "一级 1",
          children: [
            {
              id: 4,
              label: "二级 1-1",
              children: [
                {
                  id: 9,
                  label: "三级 1-1-1",
                },
                {
                  id: 10,
                  label: "三级 1-1-2",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 5,
              label: "二级 2-1",
            },
            {
              id: 6,
              label: "二级 2-2",
            },
          ],
        },
        {
          id: 3,
          label: "一级 3",
          children: [
            {
              id: 7,
              label: "二级 3-1",
            },
            {
              id: 8,
              label: "二级 3-2",
            },
          ],
        },
      ],
      loading: false
    };
  },
  created() {
    this.bumenglist();
  },
  methods: {
    refresh() {
      const t = this
      t.loading = true
      setTimeout(function() {
        t.loading = false
      }, 2000)
    },
    quxiaotj() {
      this.dialogVisible = false;
      this.$message({
        message: "已取消",
      });
    },
    quxiaobj() {
      this.dialogVisiblebj = false;
      this.$message({
        message: "已取消",
      });
    },
    bumenglist() {
      var data = {
        page: 1,
        limit: 100,
        token: localStorage.getItem("token"),
      };
      postData("department/indexList", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.roles = res.data.data;
        } else {
        }
      });
    },
    getCheckedNodes() {
      console.log(this.$refs.tree.getCheckedNodes());
    },
    getCheckedKeys() {
      console.log(this.$refs.tree.getCheckedKeys());
    },
    setCheckedNodes() {
      this.$refs.tree.setCheckedNodes([
        {
          id: 5,
          label: "二级 2-1",
        },
        {
          id: 9,
          label: "三级 1-1-1",
        },
      ]);
    },
    setCheckedKeys() {
      this.$refs.tree.setCheckedKeys([3]);
    },
    resetChecked() {
      this.$refs.tree.setCheckedKeys([]);
    },
    handleAddRole() {
      this.dialogVisible = true;
      this.newRole = { name: "" };
    },
    handleAddRolefenpei() {
      this.dialogVisiblefenpei = true;
      //   this.newRole = { name: "" };
    },
    addRole() {
      var data = {
        name: this.newRole.name,

        token: localStorage.getItem("token"),
      };
      postData("department/operation", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.bumenglist();
          this.$message({
            message: "添加成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，这是一条错误消息");
        }
      });
      this.dialogVisible = false;
      this.newRole.name = "";
    },
    addRolebj() {
      var data = {
        name: this.newRoles.name,
        id: this.newRoles.id,
        token: localStorage.getItem("token"),
      };
      postData("department/operation", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.bumenglist();
          this.$message({
            message: "编辑成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，这是一条错误消息");
        }
      });
      this.dialogVisiblebj = false;
    },
    handleEdit(row) {
      this.newRoles.name = row.name;
      this.newRoles.id = row.id;
      // 编辑操作
      this.dialogVisiblebj = true;
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除操作
          var data = {
            ids: row.id,
            token: localStorage.getItem("token"),
          };
          postData("department/del", data).then((res) => {
            // 处理响应数据
            if (res.code == 0) {
              console.log(res);
              this.bumenglist();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            } else {
              this.$message.error("错了哦，这是一条错误消息");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
  <style scoped>
.ipt_a {
  width: 300px;
  height: 50px;
  float: left;
  margin-left: 50px;
  margin-top: 20px;
}
</style>